package cn.itcast.bos.utils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;

import javax.lang.model.type.PrimitiveType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/23.
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
    //模型驱动
    protected  T model;
    @Override
    public T getModel() {
        return model;
    }
    //构造器完成model实例化
    public BaseAction(){
        Type genericSuper =  this.getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuper;
        Class<T> clazz = (Class<T>) parameterizedType.getActualTypeArguments()[0];
        try {
            model = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    //关于分页查询的方法
    protected int page ;
    protected int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }
    public void pushPageDataToValueStack(Page<T> pageData){
        Map<String ,Object> result = new HashMap<>();
        result.put("total",pageData.getTotalElements());
        result.put("rows",pageData.getContent());

        //压如栈顶
        ActionContext.getContext().getValueStack().push(result);
    }
}
